package org.ourgrid.broker.commands.executors;

import org.ourgrid.broker.commands.SchedulerData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import br.edu.ufcg.lsd.commune.container.logging.CommuneLogger;
import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;

public class LoggerExecutor extends SchedulerCommandExecutor {
	
	public static final int DEBUG = 0;
	public static final int WARN = 1;
	public static final int ERROR = 2;
	public static final int INFO = 3;
	public static final int TRACE = 4;
	public static final int FATAL = 5;
	
	private String message;
	private int type;

	/**
	 * <LOGGER message='String' type='int'/>
	 */
	public void execute(SchedulerData data, ServiceManager manager) {
		
		CommuneLogger logger = manager.getLog();
		
		fillData(data.getXml());
		
		switch (type) {
		
		case DEBUG:
			logger.debug(message);
			break;
			
		case WARN:
			logger.warn(message);
			break;
			
		case ERROR:
			logger.error(message);
			break;
			
		case INFO:
			logger.info(message);
			break;
			
		case TRACE:
			logger.trace(message);
			break;
			
		case FATAL:
			logger.fatal(message, new Throwable(message));
			break;
			
		default:
			return;
		}
	}
	
	private void fillData(String xml) {
		
		Document logDocFile = super.getXMl(xml);
		
		Element element = logDocFile.getDocumentElement();
		
		message = element.getAttribute("message");
		type = Integer.valueOf(element.getAttribute("type"));
	}
}
